package com.omesoft.hksnore.util.dao;

import android.content.Context;
import android.database.Cursor;

import com.omesoft.hksnore.util.dbhelper.DBHelperUtil;
import com.omesoft.hksnore.util.dbhelper.SetData;
import com.omesoft.hksnore.util.entity.FamilyDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Omesoft on 2017/3/2.
 */

public class FamilyIfcImpl implements FamilyIfc{
    DBHelperUtil db = null;
    String table = SetData.TABLE_NAME_MEMBER_FAMILY;
    Context context;

    public FamilyIfcImpl(Context _context){
        this.context = _context;
        db = DBHelperUtil.getInstance(context,SetData.DATATBASE_NAME_INFANETTE);
    }

    @Override
    public FamilyDTO findFamily() {
        Cursor cursor = db.findAll(table);
        FamilyDTO family = null;
        if (cursor.moveToNext()){
            family = new FamilyDTO();
            family.setFamily_id(cursor.getInt(cursor.getColumnIndexOrThrow("family_id")));
            family.setMember_id(cursor.getInt(cursor.getColumnIndexOrThrow("member_id")));
            family.setGender(cursor.getInt(cursor.getColumnIndexOrThrow("gender")));
            family.setAvatar(cursor.getString(cursor.getColumnIndexOrThrow("avatar")));
            family.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
            family.setBirthday(cursor.getString(cursor.getColumnIndexOrThrow("birthday")));
            family.setDetice_mac(cursor.getString(cursor.getColumnIndexOrThrow("device_mac")));
            family.setHeight(cursor.getInt(cursor.getColumnIndexOrThrow("height")));
            family.setWeight(cursor.getInt(cursor.getColumnIndexOrThrow("weight")));
            family.setPhone(cursor.getInt(cursor.getColumnIndexOrThrow("phone")));
            family.setReal_name(cursor.getString(cursor.getColumnIndexOrThrow("real_name")));
            family.setHipline(cursor.getInt(cursor.getColumnIndexOrThrow("hipline")));
        }


        return family;
    }

    @Override
    public List<FamilyDTO> findAll() {
        List<FamilyDTO> familys = new ArrayList<FamilyDTO>();
        Cursor cursor = db.findAll(table);
        while (cursor.moveToNext()) {
            FamilyDTO FamilyDTO = getFamily(cursor);
            familys.add(FamilyDTO);
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return familys;
    }

    @Override
    public void insert(FamilyDTO family){
        db.insertFamily(family);
    }

    @Override
    public void deleteByID(int ID) {

    }

    @Override
    public void updateFamily(FamilyDTO family) {
            db.updateFamily(family);
    }

    @Override
    public void deleteTable() {
            db.deleteTable(table);
    }
    private FamilyDTO getFamily(Cursor cursor) {
        FamilyDTO FamilyDTO = new FamilyDTO();
        FamilyDTO.setFamily_id(cursor.getInt(cursor.getColumnIndexOrThrow("family_id")));
        FamilyDTO.setBirthday(cursor.getString(cursor.getColumnIndexOrThrow("birthday")));
        FamilyDTO.setGender(cursor.getInt(cursor.getColumnIndexOrThrow("gender")));
        FamilyDTO.setHeight(cursor.getInt(cursor.getColumnIndexOrThrow("height")));
        FamilyDTO.setHipline(cursor.getInt(cursor.getColumnIndexOrThrow("hipline")));
        FamilyDTO.setMember_id(cursor.getInt(cursor.getColumnIndexOrThrow("member_id")));
        FamilyDTO.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
        FamilyDTO.setWeight(cursor.getInt(cursor.getColumnIndexOrThrow("weight")));
        FamilyDTO.setAvatar(cursor.getString(cursor.getColumnIndexOrThrow("avatar")));
        FamilyDTO.setPhone(cursor.getInt(cursor.getColumnIndexOrThrow("phone")));
        FamilyDTO.setReal_name(cursor.getString(cursor.getColumnIndexOrThrow("real_name")));
        FamilyDTO.setDetice_mac(cursor.getString(cursor.getColumnIndexOrThrow("device_mac")));
        return FamilyDTO;
    }
}
